﻿using IBLL;
using IDAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UpFilesController : ControllerBase
    {
        public UpFilesController(IWebHostEnvironment web,IOrganizationBLL bll,ILogger<UpFilesController> logger)
        {
            Web = web;
            Bll = bll;
            Logger = logger;
        }

        public IWebHostEnvironment Web { get; }
        public IOrganizationBLL Bll { get; }
        public ILogger<UpFilesController> Logger { get; }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="files">表单文件</param>
        /// <returns>返回一个虚拟路径</returns>
        [HttpPost]
        public IActionResult UpImgs(IFormFile files) 
        {
            try
            {
                string s = DateTime.Now.ToFileTime().ToString();
                string path = Web.WebRootPath + @"\img\" + s + files.FileName;
                using (FileStream fs = System.IO.File.Create(path))
                {
                    files.CopyTo(fs);
                    fs.Close();
                }
                Logger.LogInformation("准备上传图片");
                return Ok("/img/" + s + files.FileName);
            }
            catch (Exception)
            {
                Logger.LogError("出现异常，抛出");
                throw;
            }
        }
        /// <summary>
        /// 批量导出excel文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpDownExcel()
        {
            try
            {
                Logger.LogInformation($"{DateTime.Now}开始导出机构信息");
                var list = Bll.GetOrga().ToList();
                XSSFWorkbook book = new XSSFWorkbook();
                ISheet sheet = book.CreateSheet("机构信息表");
                IRow row = sheet.CreateRow(0);
                row.CreateCell(1).SetCellValue("序号");
                row.CreateCell(2).SetCellValue("机构名称");
                row.CreateCell(3).SetCellValue("机构封面");
                row.CreateCell(4).SetCellValue("特色服务");
                row.CreateCell(5).SetCellValue("营业时间");
                row.CreateCell(6).SetCellValue("机构地址");
                row.CreateCell(7).SetCellValue("联系电话");
                row.CreateCell(8).SetCellValue("发布状态");
                row.CreateCell(9).SetCellValue("机构详情");
                row.CreateCell(10).SetCellValue("是否删除");
                int num = 1;
                foreach (var i in list)
                {
                    IRow rows = sheet.CreateRow(num);
                    rows.CreateCell(1).SetCellValue(i.Oid);
                    rows.CreateCell(2).SetCellValue(i.Oname);
                    rows.CreateCell(3).SetCellValue(i.Oimg);
                    rows.CreateCell(4).SetCellValue(i.Sid);
                    rows.CreateCell(5).SetCellValue(i.StrarServiceTime);
                    rows.CreateCell(6).SetCellValue(i.Address);
                    rows.CreateCell(7).SetCellValue(i.Phone);
                    rows.CreateCell(8).SetCellValue(i.IsPublish ? "发布" : "未发布");
                    rows.CreateCell(9).SetCellValue(i.Onote);
                    rows.CreateCell(10).SetCellValue(i.IsDel ? "删除" : "未删除");
                    num++;
                }
                byte[] b;
                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    b = ms.ToArray();
                }
                return File(b ,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "机构信息表.xlsx");
            }
            catch (Exception)
            {
                Logger.LogError("出现异常，抛出");
                throw;
            }
        }

    }
}
